<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$uniacid=$_SESSION['uniacid'];
$return['set']=$this->plugin_setting('basic');
$mid=$_SESSION['uid'];
if($_RQ['op']=='main'){
	$id=$_RQ['id'];
	if(!$id){
		exi('请选择商品','error','jy_sendgoods/index/');
	}
	$_SESSION['goodsid'] = $id;
	$act = pdo_get('jy_sendgoods_act',array('id'=>$id));
	$goods=pdo_get('jy_weishop_goods',array('id'=>$act['goodsid']));
	//规格
	$specs_list=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$act['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
	//已选规格
	$selectspecs = array();
	$selespename = '';
	if($specs_list){
			foreach($specs_list as $k=>&$sp){
				$sp['items']=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$sp['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','paixu asc');
				$selectspecs[$k]=$sp['items'][0]['id'];
				$selespename .=$sp['items'][0]['title'].'，';
			}
	}
	//默认已选参数：
	$spsids = '';
	foreach($selectspecs as $k=>&$s){
			if($k==0){
				$spsids .= $s;
			}else{
				$spsids .= '_'.$s;
			}
	}
	$return['options'] = pdo_get('jy_weishop_goods_option',array('goodsid'=>$id,'specs'=>$spsids));
	//默认商品规格价格：
	if($return['options']){
		$goods['price']=$return['options']['price'];
	}
	$return['selespename'] = $selespename;
	$option_list=pdo_getall('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$id),array('id','specs','thumb','stock','price','productprice','costprice','goodssn','productsn','weight'),'','paixu asc');
	if($option_list){
			foreach($option_list as &$i){
				$i['specs']=explode('_',$i['specs']);
				foreach($i['specs'] as $j){
					$op=pdo_get('jy_weishop_goods_spec_item',array('id'=>intval($j)),array('specid','title'));
					$spec_title=pdo_getcolumn('jy_weishop_goods_spec',array('id'=>$op['specid']),'title');
					$i[$spec_title]=$op['title'];
				}
				$i['num']=$i['stock'];
				$i['old_price']=$i['productprice'];
				$i['cost_price']=$i['costprice'];
				$i['bm']=$i['goodssn'];
				$i['tm']=$i['productsn'];
				$i['image']=$i['thumb'];
			}
			$specs_list['option']=$option_list;
	}
	$return['specs_list'] = $specs_list;
	//限购
	//所有订单
//	$sql = "SELECT SUM(buynum) FROM " . tablename('jy_sendgoods_order') . " WHERE uniacid=:uniacid AND actid=:actid";
  //  $number = pdo_fetch($sql,array(":uniacid" => $_SESSION['uniacid'],":actid" =>$id));	
	//$canbuy=$act['buynum']-$number['SUM(buynum)'];
	
	//购买成功的订单
	$orders=pdo_getall('jy_sendgoods_order',array('uniacid'=>$uniacid,'ispay'=>'1','actid'=>$act['id']));
	if($orders){
		foreach($orders as &$order){
			$order['member']=pdo_get('core_members',array('uniacid'=>$uniacid,'id'=>$mid));
		}
	}
	$goods['images']=json_decode($goods['images'],true);
	if(!count($goods['images'])){
		$goods['images'][]=$goods['image'];
	}
	//判断状态
	$status=$act['status'];
	if($act['starttime']>time()){
		$status=1;
		

	}
	if($act['endtime']<time()){
		$status=3;
	}
	if($goods['total']<1){
		$status=4;
	}
	//POST提交：
	$post = array();
	$post['num'] = 1;
	$post['goodsid'] = $id;
	$post['specid']=$selectspecs;
	$return['post']=$post;
	$orders['count']=count($orders);
	$return['orders']=$orders;
	$return['status']=$status;
	$return['act']=$act;
	$return['goods']=$goods;
	$return['num']=1;
	$return['canbuy']=$canbuy;
	set_html('title','商品-'.$act['title']);
	exi($return);
}
if($_RQ['op']=='get_optionsid'){
	$post=get_postdata();
	$id=$_SESSION['goodsid'];
	if(!$id){
		exi('请先选择商品！','error');
	}

	//规格
	$specids=explode('_',$post['specid']);
	
	//已选规格
	$selespename = '';
	if($specids){
			foreach($specids as $k=>$sp){
				$spec_item=pdo_get('jy_weishop_goods_spec_item',array('id'=>$sp,'uniacid'=>$_SESSION['uniacid']),array('id','title'));
				$selespename .=$spec_item['title'].'，';
			}
	}
	$return['options'] = pdo_get('jy_weishop_goods_option',array('goodsid'=>$id,'specs'=>$post['specid']));
	//默认商品规格价格：
	if($return['options']){
		$goods['price']=$return['options']['price'];
	}
	$return['selespename'] = $selespename;
	exi($return);
}
if($_RQ['op']=='check'){
	$post=get_postdata();
	$order=pdo_get('jy_sendgoods_order',array('uniacid'=>$uniacid,'mid'=>$_SESSION['uid'],'status'=>0,'ispay'=>0,'actid'=>$post['actid']));
	if($order){
		$return['type']=2;
	}else{
		$return['type']=1;
	}
	$return['actid']=$post['actid'];
	$return['price']=$post['price'];
	$return['num']=$post['num'];
	$return['specid']=$post['specid'];
	exi($return);
}